package com.ym.b2b.product.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.product.dao.EmBProductPromoDAO;
import com.ym.b2b.product.dao.ProductDao;
import com.ym.b2b.product.service.EmBProductPromoService;
import com.ym.b2b.product.sqlbean.EmBProductPromo;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

@Controller
@RequestMapping("/product")
public class ManagerProductPromoController extends BaseController {

    @Autowired
    private EmBProductPromoService emBProductPromoService;

    @Autowired
    private EmBProductPromoDAO     emBProductPromoDAO;

    @Autowired
    private ProductDao             productDao;

    @RequestMapping("searchPageProductPromoList.do")
    @ResponseBody
    public Result searchPageProductPromoList(EmBProductPromo emBProductPromo, Page<EmBProductPromo> page) {
        Result result = new Result();
        try {
            result = emBProductPromoService.searchPageProductSimple(emBProductPromo, page);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "searchPageProductPromoListExcel.do")
    @ResponseBody
    public Result searchPageProductPromoListExcel(EmBProductPromo emBProductPromo, Page<EmBProductPromo> page,
                                                  String loadType) {
        Result result = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                result.setCode(Result.CODE_100);
                result.setResult("loadType 参数 '数量'  不能为空!");
                return result;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            result = emBProductPromoService.searchPageProductSimple(emBProductPromo, page);
            ExcelUtil<EmBProductPromo> excelUtil = new ExcelUtil<EmBProductPromo>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", result.getRows());
            result = excelUtil.createExcel("productPromoListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("insertProductPromo.do")
    @ResponseBody
    public Result insertProductPromo(EmBProductPromo emBProductPaperBatch, HttpServletRequest req) {
        Result ret = new Result();
        try {
            ret = emBProductPromoService.insertEmBProductPromo(emBProductPaperBatch);
        } catch (Exception e) {
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    @RequestMapping("updateProductPromo.do")
    @ResponseBody
    public Result updateProductPromo(EmBProductPromo emBProductPaperBatch, HttpServletRequest req) {
        Result ret = new Result();
        try {
            ret = emBProductPromoService.updateProductPromo(emBProductPaperBatch);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    @RequestMapping("deleteProductPromo.do")
    @ResponseBody
    public Result deleteProductPromo(EmBProductPromo emBProductPaperBatch, HttpServletRequest req) {
        Result ret = new Result();
        try {
            emBProductPromoDAO.deleteEmBProductPromo(emBProductPaperBatch);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    @RequestMapping("showProductPromoDetail.do")
    @ResponseBody
    public Result showProductPromoDetail(EmBProductPromo emBProductPaperBatch, HttpServletRequest req) {
        Result ret = new Result();
        try {
            EmBProductPromo d = emBProductPromoDAO.searchEmBProductPromo(emBProductPaperBatch);
            ret.putData("detail", d);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }
}
